﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace sy1_8
{
    public partial class Form1 : Form
    {
        private int remainingSeconds;

        public Form1()
        {
            InitializeComponent();
            InitializeManualTimer();
        }

        private void InitializeManualTimer()
        {
            timer1 = new Timer();
            timer1.Interval = 1000;
            timer1.Tick += timer1_Tick;
            timer1.Enabled = false;

            Console.WriteLine("手动Timer创建完成");
        }


        private void Form1_Load(object sender, EventArgs e)
        {
            if (int.TryParse(txtSeconds.Text, out remainingSeconds))
            {
                lblCountDown.Text = remainingSeconds.ToString();
            }
            timer1.Interval = 1000;
            timer1.Enabled = false;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (timer1.Enabled)
            {
                timer1.Stop();
                btnStart.Text = "计时";
                return;
            }

            if (!int.TryParse(txtSeconds.Text, out remainingSeconds) || remainingSeconds <= 0)
            {
                MessageBox.Show("请输入有效的正整数！", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            timer1.Start();
            btnStart.Text = "停止";
            lblCountDown.Text = remainingSeconds.ToString();

            Console.WriteLine($"开始计时: {remainingSeconds}秒");
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            remainingSeconds--;
            lblCountDown.Text = remainingSeconds.ToString();

            Console.WriteLine($"Tick: 剩余 {remainingSeconds}秒");

            if (remainingSeconds <= 0)
            {
                timer1.Stop();
                btnStart.Text = "计时";
                MessageBox.Show("时间到！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Console.WriteLine("计时结束");
            }
        }
    }
}